class: title-slide, inverse, right, top background-image: url(data:image/png;base64,#02_img/logo-uc.png) background-position: 7% 13% background-size: 14%, cover <br> .right[ # Semana 2 ### <br> Especificaciones de Regresión Lineal ] <br> <br> <br> <br> .left[DCDPP - Datos para la evaluación de PolÃticas Públicas | PUC | 26 de septiembre, 2022] <hr> .left[
<b>Pablo A. Celhay</b> | [
pacelhay@uc.cl](mailto:pacelhay@uc.cl) ] --- layout:true <div class="my-footer"> <span style="width:100%; text-align:center">
Semana 2|
<a href=mailto:pacelhay@uc.cl style="color: white"> pacelhay@uc.cl |
PUC-Escuela de Gobierno 2022</a> </span> </div> --- name: sl0 class: inverse middle animated, fadeIn #.pull-left[.center-l[Outline]] .pull-right[ .center-r[ .large[ 1. [Sesgo por variable omitida](#bias) 2. [Modelo general de Regresión Múltiple](#lm) - [Bondad de ajuste](#ajuste) - [Supuestos](#sup) - [Variables dummy](#dum) - [Test de hipótesis](#test) 3. [Regresión no lineal](#nlm) - [Interacciones](#int) - [Logaritmos](#log) - [Especificaciones cuadráticas](#sqr) 4. [Modelo de probabilidad lineal](#mpl) 5. [Modelo probit](#probit) 6. [Modelo logit](#logit) ]]] --- name: bias class: inverse middle center animated, fadeIn ## 1. Sesgo por variable omitida --- class: animated, fadeIn ### Sesgo por variable omitida <br> Supuesto principal para estimación insesgada: -- `$$E[\mu_i| X_i]=0$$` -- `\(\rightarrow\)` `\(\mu_i, X_i\)` son independientes `\(\rightarrow\)` Si hay otras variables en `\(\mu_i\)` que se relacionan con `\(X_i\)` entonces `\(\rightarrow\)` sesgo -- <br> **Si (S1) no se cumple entonces este sesgo le llamamos "sesgo por variable omitida"** --- class: animated, fadeIn ### Sesgo por variable omitida - La FRP de interés es: `$$Y_i=\beta_0 + \beta_1 X_{1i} + \beta_2 X_{2i} + \mu_i$$` <br> - Pero estimamos: `$$Y_i=\alpha_0 + \alpha_1 X_{1i} + v_i$$` <br> - ¿Cómo es la relación de `\(\hat{\alpha}_1\)`, nuestro estimador, con `\(\beta_1\)`, el valor verdadero? --- class: animated, fadeIn ### Sesgo por variable omitida - Recordemos que para que `\(\hat{\alpha}_1\)` recupere el valor verdadero del "efecto" de `\(X_{1}\)` sobre `\(Y\)` entonces `\(\hat{\alpha}_1\)` debe ser un estimador insesgado de `\(\beta_1\)`. `\(\rightarrow\)` `\(E[\hat{\alpha}_1] = \beta_1\)` -- - `\(\hat{\alpha}_1\)` estará sesgado siempre cuando: 1. `\(X_{2i}\)` es relevante para `\(Y_i\)` 2. `\(X_{2i}\)` esta correlacionado(a) con `\(X_{1i}\)` -- - Si (1) **y** (2) se cumplen entonces `\(\hat{\alpha}_1\)` es un estimador sesgado de `\(\beta_1\)`. - Esto es crucial al mirar un reporte o estudio con regresión lineal que afirma estimar un efecto causal - TÃpicamente no se controla por este potencial sesgo porque a veces no tenemos datos sobre `\(X_{2i}\)` (no observable) --- class: animated, fadeIn ### Sesgo por variable omitida - Magnitud del sesgo: `\(Sesgo= \alpha_1 - \beta_1\)` - Corramos (imaginariamente) la siguiente regresión `$$X_{2i}=\gamma_0 + \gamma_1 X_{1i} + w_i$$` - ¿Qué es `\(\gamma_1\)`? -- - Sustituir esto en la FRP y obtenemos: `\(\alpha_1 = \beta_1 + \gamma_1 \cdot \beta_2\)` -- - Sustituir esto fórmula de sesgo y obtenemos: `\(Sesgo= \gamma_1 \cdot \beta_2\)` -- **Discusión en clase:** ¿De qué depende el sesgo? --- class: animated, fadeIn ### Sesgo por variable omitida - Magnitud del sesgo: `\(Sesgo= \alpha_1 - \beta_1 = \gamma_1 \cdot \beta_2\)` - Si `\(\gamma_1=0\)` o `\(\beta_2=0\)` entonces no hay sesgo por variable omitida: - Intuición 1. `\(\beta_2=0\)`: Entonces `\(X_{2}\)` no deberÃa ser parte del modelo inicialmente 2. `\(\gamma_=0\)`: Entonces no hay correlación entre `\(X_{1}\)` y `\(X_2\)` - En términos de magnitudes, ¿un sesgo grande de qué depende? <img src="data:image/png;base64,#02_img/fig1.png" width="65%" style="display: block; margin: auto;" /> --- class: animated, fadeIn ### Sesgo por variable omitida - Signo del sesgo: - El signo o la dirección del sesgo depende de los signos de `\(\gamma_1\)` y `\(\beta_2\)` - Lo primero a notar es que el signo de `\(\gamma_1\)` es el signo de la correlación entre `\(X_1\)` y `\(X_2\)`, entonces el signo del sesgo depende de `\(corr(X_1,X_2)\)` y de `\(\beta_2\)`. - Dado que `\(\beta_2\)` es un parámetro poblacional no podemos saber exactamente si es positivo o negativo. Y si `\(X_2\)` no se observa no podemos saber exactamente cual es la `\(corr(X_1,X_2)\)`. - Sin embargo podemos hacer algunas conjeturas basándonos en la intuición sobre el problema bajo estudio <table class="table table-striped table-hover" style="width: auto !important; margin-left: auto; margin-right: auto;"> <caption><b>Cuadro 1: Signo del sesgo por variable omitida<b></b></b></caption> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> \(corr(X_{1}, X_{2})>0\) </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> \(corr(X_{1}, X_{2})>0\) </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;width: 5em; border-right:1px solid;"> \(\beta_{2}>0\) </td> <td style="text-align:center;width: 5em; border-right:1px solid;"> </td> <td style="text-align:center;width: 5em; "> </td> </tr> <tr> <td style="text-align:left;width: 5em; border-right:1px solid;"> \(\beta_{2}<0\) </td> <td style="text-align:center;width: 5em; border-right:1px solid;"> </td> <td style="text-align:center;width: 5em; "> </td> </tr> </tbody> </table> --- class: animated, fadeIn ### Sesgo por variable omitida <br> - Para tener una idea y afirmar si es que una variable omitida estarÃa generando una subestimación o sobre estimación del efecto es necesario conocer el signo del sesgo y además el signo de `\(\beta_1\)` <br> - Si es que el sesgo have que nuestro estimador sea mayor (en valor absoluto) que lo que deberÃa ser estamos sobre estimando el efecto de `\(X\)` sobre `\(Y\)`. Esto pasa cuando `\(\beta_1\)` y el sesgo tienen el mismo signo --- class: animated, fadeIn ### Sesgo por variable omitida: Ejemplo 1 Estudiaremos la efectividad de un programa de control prenatal antes de los 2 meses de embarazo sobre el peso del niño o niña al nacer. Variables: - `bwght`: peso al nacer (en gramos) - `monpre_bin`: `\(=1\)` si recibe tratamiento; `\(=0\)` si recibe tratamiento. - `cigs`: Promedio de consumo diario de cigarrillos <table class="table table-striped table-hover table-condensed table-responsive" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Variable </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Obs </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Media </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Std </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Min </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Max </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;background-color: white !important;"> bwght </td> <td style="text-align:center;background-color: white !important;"> 1827 </td> <td style="text-align:center;background-color: white !important;"> 3401.600 </td> <td style="text-align:center;background-color: white !important;"> 576.919 </td> <td style="text-align:center;background-color: white !important;"> 360 </td> <td style="text-align:center;background-color: white !important;"> 5204 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> monpre_bin </td> <td style="text-align:center;background-color: white !important;"> 1827 </td> <td style="text-align:center;background-color: white !important;"> 0.767 </td> <td style="text-align:center;background-color: white !important;"> 0.423 </td> <td style="text-align:center;background-color: white !important;"> 0 </td> <td style="text-align:center;background-color: white !important;"> 1 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> cigs </td> <td style="text-align:center;background-color: white !important;"> 1827 </td> <td style="text-align:center;background-color: white !important;"> 1.092 </td> <td style="text-align:center;background-color: white !important;"> 4.227 </td> <td style="text-align:center;background-color: white !important;"> 0 </td> <td style="text-align:center;background-color: white !important;"> 40 </td> </tr> </tbody> </table> --- class: animated, fadeIn ### Sesgo por variable omitida: Ejemplo 1 - Signo del sesgo: - El signo o la dirección del sesgo depende de los signos de `\(\gamma_1\)` y `\(\beta_2\)` - Lo primero a notar es que el signo de `\(\gamma_1\)` es el signo de la correlación entre `\(X_1\)` y `\(X_2\)`, entonces el signo del sesgo depende de `\(corr(X_1,X_2)\)` y de `\(\beta_2\)`. - Dado que `\(\beta_2\)` es un parámetro poblacional no podemos saber exactamente si es positivo o negativo. Y si `\(X_2\)` no se observa no podemos saber exactamente cual es la `\(corr(X_1,X_2)\)`. <br> **Discusión en clase:** ¿Qué podrÃamos saber con estos datos? -- <br> Correlación entre edad (omitida) y tratamiento `\((X_1)\)` --- class: animated, fadeIn ### Sesgo por variable omitida: Ejemplo 1 Correlación entre variable omitida `\((X_2)\)` y tratamiento `\((X_1)\)` -- ```r cor.test(df$monpre_bin, df$cigs, method = "pearson") ``` ``` Pearson's product-moment correlation data: df$monpre_bin and df$cigs t = -4, df = 1716, p-value = 0.0003 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: -0.1340 -0.0401 sample estimates: cor -0.0873 ``` --- class: animated, fadeIn ### Sesgo por variable omitida: Ejemplo 1 ```r df %>% group_by(monpre_bin) %>% dplyr::summarise(Obs=n(), Media=mean(cigs, na.rm=TRUE), Std=sd(cigs, na.rm=TRUE), Min=min(cigs, na.rm=TRUE), Max=max(cigs, na.rm=TRUE)) ``` ``` # A tibble: 2 × 6 monpre_bin Obs Media Std Min Max <dbl> <int> <dbl> <dbl> <int> <int> 1 0 426 1.75 5.46 0 40 2 1 1401 0.884 3.73 0 40 ``` --- class: animated, fadeIn ### Sesgo por variable omitida: Ejemplo 1 ```r summary(lm_robust(bwght ~ monpre_bin, data = df, se_type = "stata"), digits=4) ``` ``` Call: lm_robust(formula = bwght ~ monpre_bin, data = df, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 3378.8 26.7 126.59 0.000 3326 3431.2 1825 monpre_bin 29.7 30.9 0.96 0.337 -31 90.3 1825 Multiple R-squared: 0.000473 , Adjusted R-squared: -7.42e-05 F-statistic: 0.921 on 1 and 1825 DF, p-value: 0.337 ``` -- **Discusión en clase_** ¿Cuál es la interpretación del coeficiente sobre `monpre_bin `? --- class: animated, fadeIn ### Sesgo por variable omitida: Ejemplo 1 ```r m1 <- lm_robust(bwght ~ monpre_bin + cigs, data = df, se_type = "stata") summary(m1) ``` ``` Call: lm_robust(formula = bwght ~ monpre_bin + cigs, data = df, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 3397.8 26.97 126.00 0.000000 3344.9 3450.69 1715 monpre_bin 32.3 31.09 1.04 0.299036 -28.7 93.27 1715 cigs -11.2 3.14 -3.58 0.000352 -17.4 -5.09 1715 Multiple R-squared: 0.00788 , Adjusted R-squared: 0.00672 F-statistic: 6.86 on 2 and 1715 DF, p-value: 0.00108 ``` -- **Discusión en clase:** ¿Cuál es la interpretación del coeficiente sobre `monpre_bin `? -- **Discusión en clase:** ¿Cambia respecto a la regresión anterior?¿Cuál es la dirección del cambio y la razón del cambio en esta dirección? --- class: animated, fadeIn ### Sesgo por variable omitida: Ejemplo 2 - Salarios, capacitación y educación - Quiero estimar una regresión de salarios sobre horas de capacitación laboral y educación -- **Discusión en clase:** ¿Cómo es la regresión? -- - Suponga que en una ciudad particular, un subsidio para talleres de capacitación grande fue ofrecido a trabajadores con bajos niveles de educación asà que educación y horas de capacitación están (¿negativa o positiva?) mente correlacionados. -- - Tenemos datos de salario y horas de capacitación solamente y estimamos la siguiente regresión: `$$salario_i = \alpha_0 + \alpha_1 horas\ cap_{i} + \varepsilon_i$$` -- **Discusión en clase:** ¿Qué tan buena es la estimación de `\(\alpha_1\)` por MCO? --- name: lm class: inverse middle center animated, fadeIn ## 2. Modelo general de Regresión Múltiple --- class: animated, fadeIn ### El modelo general de regresión múltiple - Hasta ahora aprendimos: - ¿Qué es una regresión? - Interpretación de coeficientes en una regresión - Cómo estimamos estos coeficientes (de variables continuas o binarias) - Test de hipótesis para un coeficiente - Condiciones bajo las cuáles estos son insesgados - Cómo conjeturar sobre la dirección y magnitud del sesgo por variable omitida -- - Ahora mantendremos todo esto pero lo generalizaremos para el caso de regresión múltiple con más de 2 variables - EspecÃficamente... con k variables, donde `\(k=1,\ldots , K\)` --- class: animated, fadeIn ### El modelo general de regresión múltiple - La FRP es: `$$Y_i = \beta_0 + \beta_1 X_{1i} + \beta_2 X_{2i} + \ldots + \beta_k X_{ki} + \mu_i$$` - Nuevamente queremos minimizar: `$$\sum\limits_i^{n} (Y_i - \hat{Y}_i)^2$$` -- **¿Qué es eso?** -- - Con: `$$\hat{Y}_i = \hat{\beta_0} + \hat{\beta_1} X_{1i} + \hat{\beta_2} X_{2i} + \ldots + \hat{\beta_k} X_{ki}$$` --- class: animated, fadeIn ### El modelo general de regresión múltiple El coeficiente asociado a `\(X_{1i}\)` mide el cambio en `\(Y_i\)` dado un aumento en `\(X_{1i}\)`, dejando constante todo el resto de las variables dle modelo `\((X_{2i}, X_{3i}, \ldots, X_{ki})\)` **Ejemplo: Peso del niño/a al nacer post tratamiento** .pull-left[ - `bwght:` birth weight, grams - `monpre_bin:` treatment - `cigs:` avg cigarettes per day - `mage:` mother's age, years - `meduc:` mother's educ, years - `npvis:` total number of prenatal visits - `fage:` father's age, years - `feduc:` father's educ, years - `omaps:` one minute apgar score - `fmaps:` five minute apgar score - `drink:` avg drinks per week - `male:` =1 if baby male ] .pull-right[ - `mblck:` =1 if mother black - `fblck:` =1 if father black - `magesq:` mage^2 - `npvissq:` npvis^2 ] --- class: animated, fadeIn ### El modelo general de regresión múltiple ``` Call: lm_robust(formula = bwght ~ ., data = df, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 1638.2495 502.564 3.260 0.001 652.495 2624.004 1595 monpre_bin 13.3727 33.086 0.404 0.686 -51.525 78.270 1595 cigs -7.4611 3.129 -2.384 0.017 -13.599 -1.323 1595 mage 49.0195 26.753 1.832 0.067 -3.455 101.494 1595 meduc -5.2332 8.515 -0.615 0.539 -21.936 11.470 1595 npvis 3.5344 10.204 0.346 0.729 -16.479 23.548 1595 fage 5.6667 3.355 1.689 0.091 -0.914 12.247 1595 feduc 6.7703 7.908 0.856 0.392 -8.741 22.282 1595 omaps 28.3770 17.492 1.622 0.105 -5.932 62.686 1595 fmaps 63.8662 36.262 1.761 0.078 -7.261 134.993 1595 drink -18.5829 26.430 -0.703 0.482 -70.424 33.259 1595 vlbw -1950.7856 150.810 -12.935 0.000 -2246.591 -1654.980 1595 male 84.0183 26.800 3.135 0.002 31.451 136.585 1595 mblck -190.6614 133.310 -1.430 0.153 -452.143 70.820 1595 fblck 203.0290 128.227 1.583 0.114 -48.482 454.540 1595 magesq -0.8710 0.440 -1.981 0.048 -1.734 -0.009 1595 npvissq 0.0932 0.322 0.289 0.773 -0.539 0.726 1595 Multiple R-squared: 0.109 , Adjusted R-squared: 0.0999 F-statistic: 22.9 on 16 and 1595 DF, p-value: <0.0000000000000002 ``` --- class: animated, fadeIn ### El modelo general de regresión múltiple **¿Qué representa el coeficiente de `cigs`?** ``` Call: lm_robust(formula = bwght ~ ., data = df, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 1638.2495 502.564 3.260 0.001 652.495 2624.004 1595 monpre_bin 13.3727 33.086 0.404 0.686 -51.525 78.270 1595 cigs -7.4611 3.129 -2.384 0.017 -13.599 -1.323 1595 mage 49.0195 26.753 1.832 0.067 -3.455 101.494 1595 meduc -5.2332 8.515 -0.615 0.539 -21.936 11.470 1595 npvis 3.5344 10.204 0.346 0.729 -16.479 23.548 1595 fage 5.6667 3.355 1.689 0.091 -0.914 12.247 1595 feduc 6.7703 7.908 0.856 0.392 -8.741 22.282 1595 omaps 28.3770 17.492 1.622 0.105 -5.932 62.686 1595 fmaps 63.8662 36.262 1.761 0.078 -7.261 134.993 1595 drink -18.5829 26.430 -0.703 0.482 -70.424 33.259 1595 vlbw -1950.7856 150.810 -12.935 0.000 -2246.591 -1654.980 1595 male 84.0183 26.800 3.135 0.002 31.451 136.585 1595 mblck -190.6614 133.310 -1.430 0.153 -452.143 70.820 1595 fblck 203.0290 128.227 1.583 0.114 -48.482 454.540 1595 magesq -0.8710 0.440 -1.981 0.048 -1.734 -0.009 1595 npvissq 0.0932 0.322 0.289 0.773 -0.539 0.726 1595 Multiple R-squared: 0.109 , Adjusted R-squared: 0.0999 F-statistic: 22.9 on 16 and 1595 DF, p-value: <0.0000000000000002 ``` --- class: animated, fadeIn ### El modelo general de regresión múltiple **¿Cómo interpretamos el coeficiente de `mage`?** ``` Call: lm_robust(formula = bwght ~ ., data = df, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 1638.2495 502.564 3.260 0.001 652.495 2624.004 1595 monpre_bin 13.3727 33.086 0.404 0.686 -51.525 78.270 1595 cigs -7.4611 3.129 -2.384 0.017 -13.599 -1.323 1595 mage 49.0195 26.753 1.832 0.067 -3.455 101.494 1595 meduc -5.2332 8.515 -0.615 0.539 -21.936 11.470 1595 npvis 3.5344 10.204 0.346 0.729 -16.479 23.548 1595 fage 5.6667 3.355 1.689 0.091 -0.914 12.247 1595 feduc 6.7703 7.908 0.856 0.392 -8.741 22.282 1595 omaps 28.3770 17.492 1.622 0.105 -5.932 62.686 1595 fmaps 63.8662 36.262 1.761 0.078 -7.261 134.993 1595 drink -18.5829 26.430 -0.703 0.482 -70.424 33.259 1595 vlbw -1950.7856 150.810 -12.935 0.000 -2246.591 -1654.980 1595 male 84.0183 26.800 3.135 0.002 31.451 136.585 1595 mblck -190.6614 133.310 -1.430 0.153 -452.143 70.820 1595 fblck 203.0290 128.227 1.583 0.114 -48.482 454.540 1595 magesq -0.8710 0.440 -1.981 0.048 -1.734 -0.009 1595 npvissq 0.0932 0.322 0.289 0.773 -0.539 0.726 1595 Multiple R-squared: 0.109 , Adjusted R-squared: 0.0999 F-statistic: 22.9 on 16 and 1595 DF, p-value: <0.0000000000000002 ``` --- class: animated, fadeIn ### El modelo general de regresión múltiple **¿Cómo interpretamos el coeficiente de `male`?** ``` Call: lm_robust(formula = bwght ~ ., data = df, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 1638.2495 502.564 3.260 0.001 652.495 2624.004 1595 monpre_bin 13.3727 33.086 0.404 0.686 -51.525 78.270 1595 cigs -7.4611 3.129 -2.384 0.017 -13.599 -1.323 1595 mage 49.0195 26.753 1.832 0.067 -3.455 101.494 1595 meduc -5.2332 8.515 -0.615 0.539 -21.936 11.470 1595 npvis 3.5344 10.204 0.346 0.729 -16.479 23.548 1595 fage 5.6667 3.355 1.689 0.091 -0.914 12.247 1595 feduc 6.7703 7.908 0.856 0.392 -8.741 22.282 1595 omaps 28.3770 17.492 1.622 0.105 -5.932 62.686 1595 fmaps 63.8662 36.262 1.761 0.078 -7.261 134.993 1595 drink -18.5829 26.430 -0.703 0.482 -70.424 33.259 1595 vlbw -1950.7856 150.810 -12.935 0.000 -2246.591 -1654.980 1595 male 84.0183 26.800 3.135 0.002 31.451 136.585 1595 mblck -190.6614 133.310 -1.430 0.153 -452.143 70.820 1595 fblck 203.0290 128.227 1.583 0.114 -48.482 454.540 1595 magesq -0.8710 0.440 -1.981 0.048 -1.734 -0.009 1595 npvissq 0.0932 0.322 0.289 0.773 -0.539 0.726 1595 Multiple R-squared: 0.109 , Adjusted R-squared: 0.0999 F-statistic: 22.9 on 16 and 1595 DF, p-value: <0.0000000000000002 ``` --- name: ajuste class: animated, fadeIn ### Bondad de ajuste: ¿Qué tan bueno es el modelo para explicar Y? ¿Qué tanto se ajusta la lÃnea estimada a los datos? `$$\begin{split} R^{2} & = \frac{\sum\limits_i^{n} (\hat{Y_i}-\bar{Y})^2}{(Y_i-\bar{Y})^2} \\ \\ & = \frac{\textit{suma de lo explicado al cuadrado}}{\textit{suma total al cuadrado}} \\ \\ & = 1 - \frac{\textit{suma de lo NO explicado al cuadrado}}{\textit{suma total al cuadrado}} \\ \\ & = 1- \frac{\sum\limits_i^{n} (\hat{\mu_i})^2}{(Y_i-\bar{Y})^2} \end{split}$$` --- class: animated, fadeIn ### Bondad de ajuste: ¿Qué tan bueno es el modelo para explicar Y? - `\(R^{2}\)` oscila entre 0 y 1 - Añadir variables al modelo no reduce el `\(R^{2}\)` y en general este aumenta - El `\(R^{2}\)` es la proporción de la variación muestral de la variable dependiente que viene explicada por las variables independientes -- **Discusión en clase:** ¿Cómo interpretamos el `\(R^{2}\)` en el caso anterior? -- **Discusión en clase:** ¿Un `\(R^{2}\)` bajo (e.g., 0.01) es "malo"?¿Es poco confiable el estimador del efecto de `\(X\)` sobre `\(Y\)` en este caso? -- - Un `\(R^{2}\)` más grande significa mejor predicción del modelo (ajuste de la lÃnea a los datos) - Un `\(R^{2}\)` más grande NO significa mayor validez interna. No está relacionado con el sesgo del parámetro estimado --- name: sup class: animated, fadeIn ### Supuestos **S1** Esperanza condicional del error es cero - `\(E[\mu_i | X_{1i}, X_{2i}, \ldots, X_{ki}]=0\)` - **Discusión en clases:** ¿Qué pasa si hay otras variables `\(k+1\)` en `\(\mu\)`? **S2** Muestra i.i.d **S3** *Outliers* son poco probables **S4** Homocedasticidad **S5** No hay multicolinealidad perfecta - Multicolinealidad perfecta: una variable `\(X\)` puede ser escrita como una función lineal de la otra Si (S1) - (S5) se cumplen entonces `\(E[\hat{\beta}_j]=\beta_j\)` `\(\forall\)` `\(j=0,1,2,\ldots, k\)` --- class: animated, fadeIn ### Supuestos: Peso del niño/a al nacer post tratamiento** .center[ <img src="data:image/png;base64,#DCPP_semana2_files/figure-html/unnamed-chunk-13-1.png" width="720" /> ] --- name: dum class: animated, fadeIn ### Variables dummy en Regresión Múltiple **Ejemplo de brechas salariales entre hombres y mujeres** ```r dw <- wage1 dw <- dw %>% mutate(male=1-female) %>% relocate(wage, female, male) glimpse(dw) ``` ``` Rows: 526 Columns: 25 $ wage <dbl> 3.10, 3.24, 3.00, 6.00, 5.30, 8.75, 11.25, 5.00, 3.60, 18.18,… $ female <int> 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1… $ male <dbl> 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0… $ educ <int> 11, 12, 11, 8, 12, 16, 18, 12, 12, 17, 16, 13, 12, 12, 12, 16… $ exper <int> 2, 22, 2, 44, 7, 9, 15, 5, 26, 22, 8, 3, 15, 18, 31, 14, 10, … $ tenure <int> 0, 2, 0, 28, 2, 8, 7, 3, 4, 21, 2, 0, 0, 3, 15, 0, 0, 10, 0, … $ nonwhite <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0… $ married <int> 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0… $ numdep <int> 2, 3, 2, 0, 1, 0, 0, 0, 2, 0, 0, 0, 2, 0, 1, 1, 0, 0, 3, 0, 0… $ smsa <int> 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1… $ northcen <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0… $ south <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0… $ west <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1… $ construc <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0… $ ndurman <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0… $ trcommpu <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0… $ trade <int> 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0… $ services <int> 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0… $ profserv <int> 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1… $ profocc <int> 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1… $ clerocc <int> 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0… $ servocc <int> 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0… $ lwage <dbl> 1.131, 1.176, 1.099, 1.792, 1.668, 2.169, 2.420, 1.609, 1.281… $ expersq <int> 4, 484, 4, 1936, 49, 81, 225, 25, 676, 484, 64, 9, 225, 324, … $ tenursq <int> 0, 4, 0, 784, 4, 64, 49, 9, 16, 441, 4, 0, 0, 9, 225, 0, 0, 1… ``` --- class: animated, fadeIn ### Variables dummy en Regresión Múltiple **Ejemplo de brechas salariales entre hombres y mujeres** ```r m3 <- lm_robust(wage ~ female, data = dw, se_type = "stata") ``` ```r summary(m3) ``` ``` Call: lm_robust(formula = wage ~ female, data = dw, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 7.10 0.251 28.24 0 6.61 7.59 524 female -2.51 0.298 -8.44 0 -3.10 -1.93 524 Multiple R-squared: 0.116 , Adjusted R-squared: 0.114 F-statistic: 71.2 on 1 and 524 DF, p-value: 0.000000000000000313 ``` --- class: animated, fadeIn ### Variables dummy en Regresión Múltiple ¿Qué pasa si agregamos la binaria para hombres? ```r m3 <- lm_robust(wage ~ female + male, data = dw, se_type = "stata") ``` ```r summary(m3) ``` ``` Call: lm_robust(formula = wage ~ female + male, data = dw, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 60798767908212 NaN NaN NaN NaN NaN 523 female -60798767908208 NaN NaN NaN NaN NaN 523 male -60798767908205 NaN NaN NaN NaN NaN 523 Multiple R-squared: 0.115 , Adjusted R-squared: 0.112 F-statistic: NA on 2 and 523 DF, p-value: NA ``` --- class: animated, fadeIn ### Variables dummy en Regresión Múltiple - Otras variables independientes: `$$\hat{salario}_i = \hat{\beta}_0 + \hat{\beta}_1\cdot mujer_i + \hat{\beta}_2 educ_i$$` -- **Discusión en clase:** ¿Cuál es la interpretación de `\(\hat{\beta}_0, \hat{\beta}_1, \hat{\beta}_2\)` ? -- **Discusión en clase:** Graficar la relación entre educación y salarios según esta ecuación --- class: animated, fadeIn ### Variables dummy en Regresión Múltiple - Variable con más de dos categorÃas - Ejemplo: Escolaridad separada en 3 grupos - Escolaridad `\(<\)` 12 - 12 `\(\geq\)` Escolaridad `\(<\)` 16 - 16 `\(\geq\)` Escolaridad -- **Discusión en clase:** ¿Por qué hacer esto y no dejar escolaridad continua? -- <table class="table table-striped table-hover" style="width: auto !important; margin-left: auto; margin-right: auto;"> <caption><b>Cuadro 2: CategorÃas<b></b></b></caption> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Esc<12 </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> 12>=Esc<16 </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> 16>=Esc </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;background-color: white !important;width: 5em; "> Lesshs </td> <td style="text-align:center;background-color: white !important;width: 5em; "> 1 </td> <td style="text-align:center;background-color: white !important;width: 5em; "> 0 </td> <td style="text-align:center;background-color: white !important;width: 5em; "> 0 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;width: 5em; "> highsch </td> <td style="text-align:center;background-color: white !important;width: 5em; "> 0 </td> <td style="text-align:center;background-color: white !important;width: 5em; "> 1 </td> <td style="text-align:center;background-color: white !important;width: 5em; "> 0 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;width: 5em; "> College </td> <td style="text-align:center;background-color: white !important;width: 5em; "> 0 </td> <td style="text-align:center;background-color: white !important;width: 5em; "> 0 </td> <td style="text-align:center;background-color: white !important;width: 5em; "> 1 </td> </tr> </tbody> </table> --- class: animated, fadeIn ### Variables dummy en Regresión Múltiple **Discusión en clase:** ¿Cómo se ve la base de datos? ```r dw <- dw %>% mutate( lesscholl=if_else(educ<12, 1, 0), highscholl=if_else(educ>=12 & educ<16, 1, 0), college=if_else(educ>=16, 1, 0) ) dw %>% select(wage, female, male, educ, lesscholl, highscholl, college) %>% glimpse() ``` ``` Rows: 526 Columns: 7 $ wage <dbl> 3.10, 3.24, 3.00, 6.00, 5.30, 8.75, 11.25, 5.00, 3.60, 18.1… $ female <int> 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1,… $ male <dbl> 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,… $ educ <int> 11, 12, 11, 8, 12, 16, 18, 12, 12, 17, 16, 13, 12, 12, 12, … $ lesscholl <dbl> 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,… $ highscholl <dbl> 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1,… $ college <dbl> 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,… ``` --- class: animated, fadeIn ### Variables dummy en Regresión Múltiple - Variable con más de dos categorÃas ```r m3 <- lm_robust(wage ~ highscholl + college, data = dw, se_type = "stata") ``` ``` Call: lm_robust(formula = wage ~ highscholl + college, data = dw, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 4.06 0.184 22.00 0 3.69 4.42 523 highscholl 1.55 0.258 6.01 0 1.05 2.06 523 college 4.89 0.511 9.57 0 3.89 5.90 523 Multiple R-squared: 0.187 , Adjusted R-squared: 0.184 F-statistic: 52.5 on 2 and 523 DF, p-value: <0.0000000000000002 ``` **Discusión en clase:** ¿Cúal es el grupo base? **Discusión en clase:** ¿Cómo interpretamos el coeficiente de `highschool`?¿Es significativo? **Discusión en clase:** ¿Por qué elegir tres categorÃas y no dos? --- class: animated, fadeIn ### Variables dummy en Regresión Múltiple Interpretando una tabla tÃpica de regresión ```r m3a <- lm_robust(wage ~ female, data = dw, se_type = "stata") m3b <- lm_robust(wage ~ male, data = dw, se_type = "stata") m3c <- lm_robust(wage ~ educ, data = dw, se_type = "stata") m3d <- lm_robust(wage ~ exper, data = dw, se_type = "stata") m3e <- lm_robust(wage ~ female + educ + exper + tenure, data = dw, se_type = "stata") ``` --- class: animated, fadeIn ### Variables dummy en Regresión Múltiple <table class="texreg" style="margin: 10px auto;border-collapse: collapse;border-spacing: 0px;color: #000000;border-top: 2px solid #000000;"> <caption>Variable dependiente: wage</caption> <thead> <tr> <th style="padding-left: 5px;padding-right: 5px;"> </th> <th style="padding-left: 5px;padding-right: 5px;">Model 1</th> <th style="padding-left: 5px;padding-right: 5px;">Model 2</th> <th style="padding-left: 5px;padding-right: 5px;">Model 3</th> <th style="padding-left: 5px;padding-right: 5px;">Model 4</th> <th style="padding-left: 5px;padding-right: 5px;">Model 5</th> </tr> </thead> <tbody> <tr style="border-top: 1px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">(Intercept)</td> <td style="padding-left: 5px;padding-right: 5px;">7.099 (0.251)<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">4.588 (0.159)<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">-0.905 (0.725)</td> <td style="padding-left: 5px;padding-right: 5px;">5.373 (0.204)<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">-1.568 (0.826)</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">female</td> <td style="padding-left: 5px;padding-right: 5px;">-2.512 (0.298)<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">-1.811 (0.254)<sup>***</sup></td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">male</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">2.512 (0.298)<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;"> </td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">educ</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">0.541 (0.061)<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">0.572 (0.061)<sup>***</sup></td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">exper</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">0.031 (0.011)<sup>**</sup></td> <td style="padding-left: 5px;padding-right: 5px;">0.025 (0.010)<sup>**</sup></td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">tenure</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">0.141 (0.028)<sup>***</sup></td> </tr> <tr style="border-top: 1px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">R<sup>2</sup></td> <td style="padding-left: 5px;padding-right: 5px;">0.116</td> <td style="padding-left: 5px;padding-right: 5px;">0.116</td> <td style="padding-left: 5px;padding-right: 5px;">0.165</td> <td style="padding-left: 5px;padding-right: 5px;">0.013</td> <td style="padding-left: 5px;padding-right: 5px;">0.364</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Adj. R<sup>2</sup></td> <td style="padding-left: 5px;padding-right: 5px;">0.114</td> <td style="padding-left: 5px;padding-right: 5px;">0.114</td> <td style="padding-left: 5px;padding-right: 5px;">0.163</td> <td style="padding-left: 5px;padding-right: 5px;">0.011</td> <td style="padding-left: 5px;padding-right: 5px;">0.359</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Num. obs.</td> <td style="padding-left: 5px;padding-right: 5px;">526</td> <td style="padding-left: 5px;padding-right: 5px;">526</td> <td style="padding-left: 5px;padding-right: 5px;">526</td> <td style="padding-left: 5px;padding-right: 5px;">526</td> <td style="padding-left: 5px;padding-right: 5px;">526</td> </tr> <tr style="border-bottom: 2px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">RMSE</td> <td style="padding-left: 5px;padding-right: 5px;">3.476</td> <td style="padding-left: 5px;padding-right: 5px;">3.476</td> <td style="padding-left: 5px;padding-right: 5px;">3.378</td> <td style="padding-left: 5px;padding-right: 5px;">3.673</td> <td style="padding-left: 5px;padding-right: 5px;">2.958</td> </tr> </tbody> <tfoot> <tr> <td style="font-size: 0.8em;" colspan="6"><sup>***</sup>p < 0.001; <sup>**</sup>p < 0.01; <sup>*</sup>p < 0.05</td> </tr> </tfoot> </table> --- name: test class: animated, fadeIn ### Test de hipótesis en Regresión Múltiple - En regresión múltiple podrÃamos estar interesadas(os) en *testear* hipótesis de manera sumultánea - Modelo con cuatro variables independientes `$$Y_{i} = \beta_0 + \beta_1 X_{1i} +\beta_2 X_{2i} +\beta_3 X_{3i} +\beta_4 X_{4i} + \mu_i$$` -- **Discusión en clase:** ¿Por qué hacer *test* de hipótesis? - La meta en regresión lineal es usar un modelo estadÃstico y una muestra - Siempre que tenemos una muestra estaremos condicionando la validez del modelo a variación muestral - Test de hipótesis nos entregan nociones de la significancia estadÃstica - Capacidad estadÃstica de nuestra estimación para explicar el modelo poblacional/verdadero --- class: animated, fadeIn ### Test de hipótesis en Regresión Múltiple - Hasta ahora esto se veÃa asÃ: - `\(H_0: \beta_2=0\)`; `\(H_A: \beta_2\neq0\)` - Usamos el `\(t-test\)` -- - Pero ahora queremos saber sobre la significancia simultánea - Usamos el `\(F-test\)`, test de **F**ischer - TÃpicamente se utiliza para revisar: 1. `\(H_0: \beta_1=0\)` y `\(\beta_2=0\)` ; `\(H_A: \beta_1\neq0\)` y/o `\(\beta_2\neq0\)` 2. `\(H_0: \beta_1=\beta_2\)` ; `\(H_A: \beta_1\neq\beta_2\)` - En esta clase nos enfocamos en el primer uso -- - Intuición para el `\(F-test\)`: - ¿*Testear* la significancia de una variable singular a la del modelo? - ¿*Testear* no linealidades en un modelo de regresión? (ej., `\(edad\)` y `\(edad^{2}\)`) - *Testear* por separado dos variables no sirve: necesitamos ajustar por la correlación entre `\(t_1\)` y `\(t_2\)` --- class: animated, fadeIn ### Test de hipótesis en Regresión Múltiple **Ejemplo:** Resultados al nacer (Apgar al minuto) y variables explicativas - `omaps:` one minute apgar score - `cigs:` avg cigarettes per day - `drink:` avg drinks per week - `meduc:` mother's educ, years - `feduc:` father's educ, years - `male:` =1 if baby male --- class: animated, fadeIn ### Test de hipótesis en Regresión Múltiple **Ejemplo:** Resultados al nacer (Apgar al minuto) y variables explicativas ```r m4 <- lm_robust(omaps ~ cigs + feduc + meduc + male, data = df, se_type = "stata") ``` --- class: animated, fadeIn ### Test de hipótesis en Regresión Múltiple ``` Call: lm_robust(formula = omaps ~ cigs + feduc + meduc + male, data = df, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 7.8507 0.20762 37.81 0.000 7.444 8.258 1664 cigs -0.0129 0.00846 -1.52 0.128 -0.029 0.004 1664 feduc 0.0240 0.01476 1.63 0.104 -0.005 0.053 1664 meduc 0.0165 0.01618 1.02 0.307 -0.015 0.048 1664 male -0.0190 0.05423 -0.35 0.726 -0.125 0.087 1664 Multiple R-squared: 0.00885 , Adjusted R-squared: 0.00647 F-statistic: 2.8 on 4 and 1664 DF, p-value: 0.0246 ``` -- **Discusión en clase:** ¿Cómo interpretamos el coeficiente de `\(\beta_{feduc}\)`? ¿Es significativo? -- **Discusión en clase:**¿Cómo interpretamos el coeficiente de `\(\beta_{meduc}\)`? ¿Es significativo? -- **Discusión en clase:** ¿Concluimos entonces que la educación de **padres en conjunto** no importa para `apgar`? --- class: animated, fadeIn ### Test de hipótesis en Regresión Múltiple Ejemplo: Resultados al nacer (Apgar al minuto) y variables explicativas **Discusión en clase:** ¿Cómo podemos diseñar un test que nos permita revisar la hipótesis de que educación de padres importa? -- - `\(H_0: \beta_{meduc}= \beta_{feduc}=0\)`; `\(H_A: \beta_{meduc}\neq 0\)` y/o `\(\beta_{feduc}\neq0\)` - Asà cómo acabamos de calcular test t para cada coeficiente por separado, ahora calculamos el test F para la significancia conjunta. -- .pull-left[ ```r linearHypothesis(m4, c("feduc = 0", "meduc = 0")) ``` ``` Linear hypothesis test Hypothesis: feduc = 0 meduc = 0 Model 1: restricted model Model 2: omaps ~ cigs + feduc + meduc + male Res.Df Df Chisq Pr(>Chisq) 1 1666 2 1664 2 8.12 0.017 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` ] .pull-right[ **Discusión en clase:** ¿Cómo interpretamos? ¿Qué concluÃmos? ] --- class: animated, fadeIn ### Test de hipótesis en Regresión Múltiple **Discusión en clase:** ¿Por qué los tes t no eran significativos y el F si? - Clave: correlación entre ambas variables aumenta los errores estándar en los coeficientes de estas variables ```r cor.test(df$feduc, df$meduc, method = "pearson") ``` ``` Pearson's product-moment correlation data: df$feduc and df$meduc t = 30, df = 1777, p-value <0.0000000000000002 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: 0.554 0.615 sample estimates: cor 0.585 ``` --- class: animated, fadeIn ### Test de hipótesis en Regresión Múltiple - Intuición - El test F se puede pensar tomando dos regresiones como ejemplo 1. Regresión restringida que impone `\(H_0\)`: `$$omaps_i = \alpha_0 + \alpha_1 cigs_i + \alpha_2 male_i + v_i$$` 2. Regresión no-restringida que no impone `\(H_0\)`: `$$omaps_i = \beta_0 + \beta_1 cigs_i + \beta_2 male_i + \beta_3 meduc_i + \beta_4 feduc_i + \mu_i$$` -- **Discusión en clase:** ¿Qué pasa con el `\(R^{2}\)` cuando pasamos de (1) a (2)? --- class: animated, fadeIn ### Test de hipótesis en Regresión Múltiple - `\(F-test\)` mira el aumento en el `\(R^{2}\)` cuando agregamos variables de interés. En otras palabras, medirá si estas variables adicionales agregan algo al modelo. 1. Si son variables relevantes: `\(R^{2}\)` deberÃa aumentar notoriamente 2. Si no son variables relevantes: `\(R^{2}\)` deberÃa \pause{} ... disminuir/aumentar poco/mucho? -- - Fórmula simple: `$$F_{q,N-k-1}=\frac{(R_{NR}^{2}-R_{R}^{2})/q}{(1-R_{NR}^{2})/(N-k-1)}$$` - **q** Número de coeficientes *testeados* - **NR** No restringido; incluye las variables a *testear* - **R** Restringido; no incluye las variables a *testear* - **N** Número de observaciones - **k** Número de variables en la versión no restringida --- class: animated, fadeIn ### Test de hipótesis en Regresión Múltiple - Fórmula simple: `$$F_{q,N-k-1}=\frac{(R_{NR}^{2}-R_{R}^{2})/q}{(1-R_{NR}^{2})/(N-k-1)}$$` - **q** Número de coeficientes *testeados* - **NR** No restringido; incluye las variables a *testear* - **R** Restringido; no incluye las variables a *testear* - **N** Número de observaciones - **k** Número de variables en la versión no restringida -- El F crÃtico cambia con q <img src="data:image/png;base64,#02_img/fig2.png" width="65%" style="display: block; margin: auto;" /> -- **Discusión en clase:** Caso anterior: ¿q, k, N, F crÃtico? --- class: animated, fadeIn ### Test de hipótesis en Regresión Múltiple ```r m4a <- lm_robust(omaps ~ cigs + male, data = na.omit(df), se_type = "stata") m4b <- lm_robust(omaps ~ cigs + male + feduc + meduc, data = na.omit(df), se_type = "stata") ``` --- class: animated, fadeIn ### Test de hipótesis en Regresión Múltiple <table class="texreg" style="margin: 10px auto;border-collapse: collapse;border-spacing: 0px;color: #000000;border-top: 2px solid #000000;"> <caption>Variable dependiente: wage</caption> <thead> <tr> <th style="padding-left: 5px;padding-right: 5px;"> </th> <th style="padding-left: 5px;padding-right: 5px;">Modelo Restrigido</th> <th style="padding-left: 5px;padding-right: 5px;">Modelo NO Restringido</th> </tr> </thead> <tbody> <tr style="border-top: 1px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">(Intercept)</td> <td style="padding-left: 5px;padding-right: 5px;">8.419 (0.035)<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">7.883 (0.206)<sup>***</sup></td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">cigs</td> <td style="padding-left: 5px;padding-right: 5px;">-0.017 (0.009)</td> <td style="padding-left: 5px;padding-right: 5px;">-0.014 (0.009)</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">male</td> <td style="padding-left: 5px;padding-right: 5px;">-0.022 (0.054)</td> <td style="padding-left: 5px;padding-right: 5px;">-0.023 (0.055)</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">feduc</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">0.025 (0.015)</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">meduc</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">0.014 (0.017)</td> </tr> <tr style="border-top: 1px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">R<sup>2</sup></td> <td style="padding-left: 5px;padding-right: 5px;">0.004</td> <td style="padding-left: 5px;padding-right: 5px;">0.009</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Adj. R<sup>2</sup></td> <td style="padding-left: 5px;padding-right: 5px;">0.003</td> <td style="padding-left: 5px;padding-right: 5px;">0.006</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Num. obs.</td> <td style="padding-left: 5px;padding-right: 5px;">1612</td> <td style="padding-left: 5px;padding-right: 5px;">1612</td> </tr> <tr style="border-bottom: 2px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">RMSE</td> <td style="padding-left: 5px;padding-right: 5px;">1.096</td> <td style="padding-left: 5px;padding-right: 5px;">1.094</td> </tr> </tbody> <tfoot> <tr> <td style="font-size: 0.8em;" colspan="3"><sup>***</sup>p < 0.001; <sup>**</sup>p < 0.01; <sup>*</sup>p < 0.05</td> </tr> </tfoot> </table> --- class: animated, fadeIn ### Test de hipótesis en Regresión Múltiple <br> **Discusión en clase:** Caso anterior: `\(R_{NT}^{2}\)`, `\(R_{R}^{2}\)` ¿? -- <br> **Discusión en clase:** ¿Cómo calculamos el F? -- ```r waldtest(m4a, m4b) ``` ``` Wald test Model 1: omaps ~ cigs + male Model 2: omaps ~ cigs + male + feduc + meduc Res.Df Df Chisq Pr(>Chisq) 1 1609 2 1607 2 7.45 0.024 * --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` --- name: nlm class: inverse middle center animated, fadeIn ## 3. Regresión no lineal --- class: animated, fadeIn ### Regresión no lineal - Por ahora hemos visto sólo ejemplos de relaciones **lineales** entre `\(Y\)` y `\(X\)`: `$$Y_i=\beta_0 + \beta_1X_i+ \mu_i$$` `$$salario_i=\beta_0 + \beta_1escol_i+ \mu_i$$` `$$ptjeacad_i=\beta_0 + \beta_1tamañoclase_i+ \mu_i$$` - Lineal `\(\rightarrow\)` efecto es el mismo para todo `\(i\)`. `\(\rightarrow\)` Poco realista **Discusión en clase:** ¿Cómo estimamos un efecto del tamaño de la clase que sea distinto para niñas y niños? - En regresiones no lineales el cambio en `\(Y\)` asociado a un cambio en `\(X_1\)` depende del valor de `\(X_1\)` o de otra variable `\(X_2\)` - Hoy: interaciones --- name: int class: animated, fadeIn ### Interacciones - Al escribir nuestra FRP, inmediatamente estamos haciendo supuestos de cómo se relacionan la variable dependiente con las independientes: `$$salario_i=\beta_0 + \beta_1escol_i+\beta_2 mujer_i + \mu_i$$` - Esta FRP permite que el salario sea distinto para hombres y mujeres - Pero no permite que la asociación entre educación y salarios sea distinta entre hombres y mujeres **Discusión en clase:** Gráficamente, ¿cómo es la relación entre educación y salarios según esta regresión? --- class: animated, fadeIn ### Interacciones **Discusión en clase:** ¿Cuál es el cambio en salarios asociado a un año adicional de educación para hombres? ¿Para mujeres? ```r m5 <- lm_robust(wage ~ female + educ, data = dw, se_type = "stata") ``` ```r summary(m5) ``` ``` Call: lm_robust(formula = wage ~ female + educ, data = dw, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 0.623 0.7287 0.855 0.393 -0.809 2.054 523 female -2.273 0.2702 -8.414 0.000 -2.804 -1.743 523 educ 0.506 0.0599 8.456 0.000 0.389 0.624 523 Multiple R-squared: 0.259 , Adjusted R-squared: 0.256 F-statistic: 69.1 on 2 and 523 DF, p-value: <0.0000000000000002 ``` --- class: animated, fadeIn ### Interacciones **Discusión en clase:** ¿Es esto realista? -- - Lo que nos gustarÃa capturar es una asociación entre salarios y educación distinta para hombres y mujeres - Para estos efectos hacemos una interacción: `$$salario_i=\beta_0 + \beta_1escol_i+\beta_2 mujer_i +\beta_3 escol_i \times mujer_i + \mu_i$$` - En `R`: `mujer_esc`=`escol`*`mujer` - La FRM entonces es: $$\hat{salario}_i=\hat{\beta}_0 + \hat{\beta_1}escol_i+\hat{\beta_2} mujer_i +\hat{\beta_3} mujer\_esc_i $$ --- class: animated, fadeIn ### Interacciones `$$\hat{salario}_i=\hat{\beta}_0 + \hat{\beta_1}escol_i+\hat{\beta_2} mujer_i +\hat{\beta_3} mujer\_esc_i$$` <table class="table table-striped table-hover" style="width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Hombre: \(mujer_i=0\) </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Mujer: \(mujer_i=1\) </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;width: 7em; "> \(\hat{salario_i}=\hat{\beta_{0}}+\hat{\beta_{1}}escol_{i}\) </td> <td style="text-align:center;width: 7em; "> \(\hat{salario_i}=(\hat{\beta_{0}}+\hat{\beta_{2}})+(\hat{\beta_{1}}+\hat{\beta_{3}})escol_{i}\) </td> </tr> </tbody> </table> <br> -- **Discusión en clase:** ¿Cuál es el cambio en salarios asociado a un cambio en escolaridad para hombres? ¿Para mujeres? -- **Discusión en clase:** ¿Cuál es la interpretación de `\(\hat{\beta_1}\)`? ¿de `\(\hat{\beta_3}\)`? --- class: animated, fadeIn ### Interacciones ```r dw <- dw %>% mutate(female_educ=educ*female) m5 <- lm_robust(wage ~ female + educ + female_educ, data = dw, se_type = "stata") ``` ```r summary(m5) ``` ``` Call: lm_robust(formula = wage ~ female + educ + female_educ, data = dw, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 0.200 0.8717 0.230 0.818 -1.512 1.913 522 female -1.199 1.4608 -0.820 0.412 -4.068 1.671 522 educ 0.539 0.0734 7.350 0.000 0.395 0.684 522 female_educ -0.086 0.1238 -0.695 0.488 -0.329 0.157 522 Multiple R-squared: 0.26 , Adjusted R-squared: 0.256 F-statistic: 49.1 on 3 and 522 DF, p-value: <0.0000000000000002 ``` -- **Discusión en clases:** ¿Cuál es el cambio en salario con el cambio en un año de escolaridad para hombres? ¿Para mujeres? --- class: animated, fadeIn ### Interacciones - Mas ejemplos: Interacción entre dos variables binarias `$$Y_i=\beta_0 + \beta_1D_{1i} + \beta_2D_{2i} + \beta_3 D_{1i}\times D_{2i}+ \mu_i$$` `$$salario_i=\beta_0 + \beta_1mujer_i + \beta_2 ecivil_i + \beta_3 ecivil_i \times mujer_i + \mu_i$$` `\(\rightarrow\)` La diferencia en salarios entre hombres y mujeres además depende del estado civil `\(\rightarrow\)` La diferencia en salarios entre personas casadas y no casadas depende de su sexo - Mas ejemplos: Interacción entre dos variables continuas `$$salario_i=\beta_0 + \beta_1educ_{i} + \beta_2 exper_{i} + \beta_3 educ_{i} \times exper_{i} + \mu_i$$` `\(\rightarrow\)` Los retornos a la educación dependen de la experiencia `\(\rightarrow\)` Los retornos a la experiencia dependen de la educación --- class: animated, fadeIn ### Interacciones - Marco general para interacción entre variables binarias `$$Y_i=\beta_0 + \beta_1D_{1i} + \beta_2D_{2i} + \beta_3 D_{1i}\times D_{2i}+ \mu_i$$` <table class="table table-striped table-hover" style="width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> \(D_{1i}=0\) </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> \(D_{1i}=1\) </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;width: 7em; "> </td> <td style="text-align:center;width: 7em; "> </td> <td style="text-align:center;width: 7em; "> </td> </tr> <tr> <td style="text-align:left;width: 7em; "> \(D_{2i}=0\) </td> <td style="text-align:center;width: 7em; "> </td> <td style="text-align:center;width: 7em; "> </td> </tr> <tr> <td style="text-align:left;width: 7em; "> \(D_{2i}=1\) </td> <td style="text-align:center;width: 7em; "> </td> <td style="text-align:center;width: 7em; "> </td> </tr> </tbody> </table> -- - Matemáticamente: - `\(\frac{\Delta Y}{\Delta X_1} = \beta_1 + \beta_3 X_2\)` - `\(\frac{\Delta Y}{\Delta X_2} = \beta_2 + \beta_3 X_1\)` --- class: animated, fadeIn ### Interacciones: Estudio de caso Diferencias en accidentes fatales de tránsito entre lugares con distinta regulación para el consumo de cerveza ```r dfat <- Ecdat::Fatality %>% filter(year==1988) %>% mutate(jaild=if_else(jaild=="yes", 1, 0), comserd=if_else(comserd=="yes", 1, 0)) ``` - `state`: state ID code - `year`: year - `mrall`: traffic fatality rate (deaths per 10000) - `beertax`: tax on case of beer - `jaild`: mandatory jail sentence ? - `comserd`: mandatory community service ? - `vmiles`: average miles per driver - `unrate`: unemployment rate - `perinc`: per capita personal income --- class: animated, fadeIn ### Interacciones: Estudio de caso Diferencias en accidentes fatales de tránsito entre lugares con distinta regulación para el consumo de cerveza <table class="table table-striped table-hover table-condensed table-responsive" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Variable </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Obs </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Media </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Std </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Min </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Max </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;background-color: white !important;"> mrall </td> <td style="text-align:center;background-color: white !important;"> 48 </td> <td style="text-align:center;background-color: white !important;"> 2.070 </td> <td style="text-align:center;background-color: white !important;"> 0.521 </td> <td style="text-align:center;background-color: white !important;"> 1.2311 </td> <td style="text-align:center;background-color: white !important;"> 3.24 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> beertax </td> <td style="text-align:center;background-color: white !important;"> 48 </td> <td style="text-align:center;background-color: white !important;"> 0.480 </td> <td style="text-align:center;background-color: white !important;"> 0.435 </td> <td style="text-align:center;background-color: white !important;"> 0.0433 </td> <td style="text-align:center;background-color: white !important;"> 2.19 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> jaild </td> <td style="text-align:center;background-color: white !important;"> 48 </td> <td style="text-align:center;background-color: white !important;"> 0.292 </td> <td style="text-align:center;background-color: white !important;"> 0.459 </td> <td style="text-align:center;background-color: white !important;"> 0.0000 </td> <td style="text-align:center;background-color: white !important;"> 1.00 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> comserd </td> <td style="text-align:center;background-color: white !important;"> 48 </td> <td style="text-align:center;background-color: white !important;"> 0.208 </td> <td style="text-align:center;background-color: white !important;"> 0.410 </td> <td style="text-align:center;background-color: white !important;"> 0.0000 </td> <td style="text-align:center;background-color: white !important;"> 1.00 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> vmiles </td> <td style="text-align:center;background-color: white !important;"> 48 </td> <td style="text-align:center;background-color: white !important;"> 8.616 </td> <td style="text-align:center;background-color: white !important;"> 1.115 </td> <td style="text-align:center;background-color: white !important;"> 5.7899 </td> <td style="text-align:center;background-color: white !important;"> 11.81 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> unrate </td> <td style="text-align:center;background-color: white !important;"> 48 </td> <td style="text-align:center;background-color: white !important;"> 5.456 </td> <td style="text-align:center;background-color: white !important;"> 1.838 </td> <td style="text-align:center;background-color: white !important;"> 2.4000 </td> <td style="text-align:center;background-color: white !important;"> 10.90 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> perinc </td> <td style="text-align:center;background-color: white !important;"> 48 </td> <td style="text-align:center;background-color: white !important;"> 14893.527 </td> <td style="text-align:center;background-color: white !important;"> 2628.106 </td> <td style="text-align:center;background-color: white !important;"> 10698.7490 </td> <td style="text-align:center;background-color: white !important;"> 22193.46 </td> </tr> </tbody> </table> --- class: animated, fadeIn ### Interacciones: Estudio de caso **Análisis de regresión** ```r m5a <- lm_robust(mrall ~ jaild, data = dfat, se_type = "stata") m5b <- lm_robust(mrall ~ jaild + beertax + comserd + vmiles + unrate + perinc, data = dfat, se_type = "stata") m5c <- lm_robust(mrall ~ jaild + beertax + comserd + vmiles + unrate + perinc + jaild:beertax, data = dfat, se_type = "stata") ``` --- class: animated, fadeIn ### Interacciones: Estudio de caso <table class="texreg" style="margin: 10px auto;border-collapse: collapse;border-spacing: 0px;color: #000000;border-top: 2px solid #000000;"> <caption>Variable dependiente mrall: traffic fatality</caption> <thead> <tr> <th style="padding-left: 5px;padding-right: 5px;"> </th> <th style="padding-left: 5px;padding-right: 5px;">Model 1</th> <th style="padding-left: 5px;padding-right: 5px;">Model 2</th> <th style="padding-left: 5px;padding-right: 5px;">Model 3</th> </tr> </thead> <tbody> <tr style="border-top: 1px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">(Intercept)</td> <td style="padding-left: 5px;padding-right: 5px;">1.961 (0.085)<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">0.577 (0.762)</td> <td style="padding-left: 5px;padding-right: 5px;">0.648 (0.816)</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">jaild</td> <td style="padding-left: 5px;padding-right: 5px;">0.371 (0.158)<sup>*</sup></td> <td style="padding-left: 5px;padding-right: 5px;">0.121 (0.164)</td> <td style="padding-left: 5px;padding-right: 5px;">0.156 (0.227)</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">beertax</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">0.145 (0.108)</td> <td style="padding-left: 5px;padding-right: 5px;">0.163 (0.113)</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">comserd</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">0.097 (0.183)</td> <td style="padding-left: 5px;padding-right: 5px;">0.103 (0.183)</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">vmiles</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">0.223 (0.049)<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">0.218 (0.054)<sup>***</sup></td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">unrate</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">0.051 (0.035)</td> <td style="padding-left: 5px;padding-right: 5px;">0.049 (0.036)</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">perinc</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">-0.000 (0.000)<sup>*</sup></td> <td style="padding-left: 5px;padding-right: 5px;">-0.000 (0.000)<sup>*</sup></td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">jaild:beertax</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">-0.077 (0.340)</td> </tr> <tr style="border-top: 1px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">R<sup>2</sup></td> <td style="padding-left: 5px;padding-right: 5px;">0.107</td> <td style="padding-left: 5px;padding-right: 5px;">0.637</td> <td style="padding-left: 5px;padding-right: 5px;">0.638</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Adj. R<sup>2</sup></td> <td style="padding-left: 5px;padding-right: 5px;">0.088</td> <td style="padding-left: 5px;padding-right: 5px;">0.584</td> <td style="padding-left: 5px;padding-right: 5px;">0.574</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Num. obs.</td> <td style="padding-left: 5px;padding-right: 5px;">48</td> <td style="padding-left: 5px;padding-right: 5px;">48</td> <td style="padding-left: 5px;padding-right: 5px;">48</td> </tr> <tr style="border-bottom: 2px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">RMSE</td> <td style="padding-left: 5px;padding-right: 5px;">0.498</td> <td style="padding-left: 5px;padding-right: 5px;">0.336</td> <td style="padding-left: 5px;padding-right: 5px;">0.340</td> </tr> </tbody> <tfoot> <tr> <td style="font-size: 0.8em;" colspan="4"><sup>***</sup>p < 0.001; <sup>**</sup>p < 0.01; <sup>*</sup>p < 0.05</td> </tr> </tfoot> </table> --- class: animated, fadeIn ### Interacciones: Estudio de caso **Discusión en clases:** ¿Cómo ocupamos esta información para deducir asociaciones entre una regulación más rÃgida sobre el consumo de cerveza y los accidentes de tránsito? -- **Discusión en clases:** ¿Es la asociación entre el impuesto a la cerveza y la tasas de accidentes igual para lugares con distintas sentencias de cárcel obligatoria? -- **Discusión en clases:** ¿Cómo *testeamos* la hipótesis de que la asociación entre impuesto y fatalidad es mayor en lugares con *alcohotest*? --- name: log class: animated, fadeIn ### Regresión no lineal <br> - Otra forma de regresión no lineal es cuando especificamos `\(Y\)` o `\(X\)` en **logaritmnos**. - Para efectos de esta clase utilizaremos siempre el logaritmo natural - La utilidad está en que la interpretación ahora es en cambios porcenatules - Por ejemplo, en vez de decir que `\(\Delta X\)` (escolaridad) genera un cambio en `\(\Delta Y\)` (invresos) de $10000 pesos vamos a poder inferir en qué porcentaje cambian los ingresos --- name: log class: animated, fadeIn ### Logaritmos <br> - Asà la interpretación es en cambios porcentuales - La relación entre logaritmo y cambios porcentuales viene de que para pequeños `\(\Delta x\)`: `$$ln(x+\Delta x) - ln(x) = \frac{\Delta x}{x}$$` - Ejemplo: `\(x=100\)`; `\(\Delta x =1\)` `$$\frac{\Delta x}{x}=0.01$$` `$$ln(101)-ln(100)=0.00995 = \frac{1}{100}=0.01=1\%$$` --- class: animated, fadeIn ### Logaritmos - Hay 3 casos para los cuales se ocupan logaritmos en regresión lineal - Caso 1: `\(X\)` está en logs, `\(Y\)` no - Caso 2: `\(Y\)` está en logs, `\(X\)` no - Ambas están en logs - Cuando usamos logaritmos: - La mecánica del OLS no cambia - La interpretación de los coeficientes cambia - Caso 1: `\(X\)` en log `$$Y=\beta_0 + \beta_1 ln(X_{1i}) + \beta_2 X_{2i} + \ldots \beta_k X_{ki} + \mu_i$$` - Interpretación: - `\(\beta_1\)`: El cambio en `\(Y\)` asociado a un cambio de in `\(1\%\)` en `\(X_{1i}\)`, manteniendo constante `\(X_{2i}, \ldots, X_{ki}\)` --- class: animated, fadeIn ### Logaritmos: Ejemplo **Relación entre puntaje académico y salarios** <img src="data:image/png;base64,#DCPP_semana2_files/figure-html/unnamed-chunk-49-1.png" width="60%" style="display: block; margin: auto;" /> -- El gráfico sugiere que la relación entre ingreso y puntaje académico no es lineal **¿por qué?** --- class: animated, fadeIn ### Logaritmos: Ejemplo - Una forma de especificar la regresión es: `$$Y=\beta_0 + \beta_1 ln(X_{i}) + \mu_i$$` `$$ptje\_acad_i=\beta_0 + \beta_1 ln(ingreso_{i}) + \mu_i$$` - De esta forma no asumimos que el cambio en pesos es el mismo en todo el rango de ingreso. Lo llevamos a cambios procentuales. Un cambio de un 1% no es lo mismo para ingresos de 200 mil que para ingresos de 1 MM. --- class: animated, fadeIn ### Logaritmos: Ejemplo En `R` ```r dw2 <- dw2 %>% mutate(lwage=log(wage)) # Logaritmo natural m6 <- lm_robust(KWW ~ lwage, data=dw2, se_type = "stata") ``` ```r summary(m6) ``` ``` Call: lm_robust(formula = KWW ~ lwage, data = dw2, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) -1.91 4.391 -0.436 0.663 -10.53 6.70 933 lwage 5.56 0.644 8.631 0.000 4.29 6.82 933 Multiple R-squared: 0.0938 , Adjusted R-squared: 0.0928 F-statistic: 74.5 on 1 and 933 DF, p-value: <0.0000000000000002 ``` --- class: animated, fadeIn ### Logaritmos: Ejemplo ``` Call: lm_robust(formula = KWW ~ lwage, data = dw2, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) -1.91 4.391 -0.436 0.663 -10.53 6.70 933 lwage 5.56 0.644 8.631 0.000 4.29 6.82 933 Multiple R-squared: 0.0938 , Adjusted R-squared: 0.0928 F-statistic: 74.5 on 1 and 933 DF, p-value: <0.0000000000000002 ``` - ¿Cómo interpretamos el coeficiente asociado a `lwage` en esta regresión? Dividir por 100 `\(\beta_1\)` - El supuesto aquà es que un aumento en un 1% en ingresos tiene el mismo efecto indiferente del nivel de ingreso. ¿Cómo cambia `\(Y\)` con? 1. un aumento de 1% de un ingreso de $10.000 es de 10.100 2. un aumento de 1% de un ingreso de $100.000 es de 101.000 --- class: animated, fadeIn ### Logaritmos <br> <table> <caption>Cambio en ptje_acad con un aumento del ingreso de:</caption> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> 10.000 a 10.100 </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> 100.000 a 101.000 </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;background-color: white !important;"> \(ptje_{acad}=\beta_0 + \beta_1log(ingreso_i)+\mu_i \) </td> <td style="text-align:center;background-color: white !important;"> </td> <td style="text-align:center;background-color: white !important;"> </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> \(ptje_{acad}=\alpha_0 + \alpha_1log(ingreso_i)+\mu_i \) </td> <td style="text-align:center;background-color: white !important;"> </td> <td style="text-align:center;background-color: white !important;"> </td> </tr> </tbody> </table> --- class: animated, fadeIn ### Logaritmos <br> - `\(ptje\_acad_i=\alpha_0 + \alpha_1 ingreso_i + \mu_i\)` asume que el ingreso y el puntaje están linealmente asociados - `\(ptje\_acad_i=\beta_0 + \beta_1 log(ingreso_i) + \mu_i\)` asume que el puntaje y el log de ingresos están linealmente asociados: - Puntaje académico y cambio porcentual en ingreso están linealmente asociados - Si `\(\beta_1\)` es positivo, el puntaje académico aumenta pero a tasas decreciente: ¿los datos sugieren esto? Para un mismo cambio en ingresos ($100) ¿es el cambio en puntaje igual o menor/mayor dependiendo del nivel de ingreso? --- class: animated, fadeIn ### Logaritmos Caso 2: `\(Y\)` en log `$$ln(Y)=\beta_0 + \beta_1 X_{1i} + \beta_2 X_{2i} + \ldots \beta_k X_{ki} + \mu_i$$` - Interpretación: - `\(\beta_1\)`: El cambio porcentual `\(Y\)` asociado a un cambio de `\(X_{1i}\)` en una unidad manteniendo constante `\(X_{2i}, \ldots, X_{ki}\)` - `\(\beta_2\)`: El cambio porcentual `\(Y\)` asociado a un cambio de `\(X_{2i}\)` en una unidad manteniendo constante `\(X_{1i}, \ldots, X_{ki}\)` - Y asà para todos los `\(X\)`s - Ejemplo: regresión de salarios `$$salario_i = \beta_0 + \beta_1 educ_i + \mu_i$$` - Asume que la asociación entre salario y educación es la misma sin importar el nivel de educación - Quizás es más realista asumir que el cambio porcentual en salarios es el mismo sin importar el nivel de educación --- class: animated, fadeIn ### Logaritmos `\(\rightarrow\)` `\(ln(salario_i) = \beta_0 + \beta_1 educ_i + \mu_i\)` ```r m7 <- lm_robust(lwage ~ educ + exper + tenure + female, data=dw, se_type = "stata") ``` ```r summary(m7) ``` ``` Call: lm_robust(formula = lwage ~ educ + exper + tenure + female, data = dw, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 0.50135 0.11526 4.35 0.000 0.275 0.728 521 educ 0.08746 0.00799 10.95 0.000 0.072 0.103 521 exper 0.00463 0.00162 2.86 0.004 0.001 0.008 521 tenure 0.01737 0.00355 4.89 0.000 0.010 0.024 521 female -0.30115 0.03767 -7.99 0.000 -0.375 -0.227 521 Multiple R-squared: 0.392 , Adjusted R-squared: 0.388 F-statistic: 73.8 on 4 and 521 DF, p-value: <0.0000000000000002 ``` --- class: animated, fadeIn ### Logaritmos ```r summary(m7) ``` ``` Call: lm_robust(formula = lwage ~ educ + exper + tenure + female, data = dw, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 0.50135 0.11526 4.35 0.000 0.275 0.728 521 educ 0.08746 0.00799 10.95 0.000 0.072 0.103 521 exper 0.00463 0.00162 2.86 0.004 0.001 0.008 521 tenure 0.01737 0.00355 4.89 0.000 0.010 0.024 521 female -0.30115 0.03767 -7.99 0.000 -0.375 -0.227 521 Multiple R-squared: 0.392 , Adjusted R-squared: 0.388 F-statistic: 73.8 on 4 and 521 DF, p-value: <0.0000000000000002 ``` **¿Cómo interpretamos estos coeficientes?** -- - Cuando la variable dependiente de un modelo es `\(log(Y)\)` hay que multiplicar todos los coeficientes por 100 e interpretarlos como el cambio porcentual --- class: animated, fadeIn ### Logaritmos Caso 3: `\(Y\)` en log y `\(X\)` en log `$$ln(Y)=\beta_0 + \beta_1 ln(X_{1i}) + \beta_2 X_{2i} + \ldots \beta_k X_{ki} + \mu_i$$` - Interpretación: - `\(\beta_1\)`: El cambio porcentual `\(Y\)` asociado a un cambio de `\(X_{1i}\)` en un 1%, manteniendo constante `\(X_{2i}, \ldots, X_{ki}\)` - Ejemplo: contaminación y precios de las casas `$$ln(precio_i) = 9.23 - 0.178 ln(conta_i) + 0.306 piezas_i \mu_i$$` - Interpretemos estos coeficientes --- name: sqr class: animated, fadeIn ### Especificación cuadrática - Intuición: El cambio en `\(Y\)` (salario) respecto a `\(X\)` (escolaridad) depende del valor de `\(X\)` `$$Y =\beta_0 + \beta_1 X_{1i} + \beta_2 X_{1i}^2 + \mu_i$$` - Mismo método de estimación pero distintas interpretación - `\(\beta_1\)`: ya no es el cambio en `\(Y\)` con un cambio en `\(X_{1i}\)` - Antes tenÃamos que: `\(\frac{\Delta Y}{\Delta X_1} = \beta_1\)` - Ahora: `\(\frac{\Delta Y}{\Delta X_1} = \beta_1 + 2 \times \beta_2 X_1\)` - Esto viene de tomar una derivada pero lo importante es que ahora el cambio en `\(Y\)` asociado a un cambio en `\(X_1\)` en una unidad depende del nivel de `\(X_1\)` - No es la misma asociación para personas con menores niveles de escolaridad que para personas con mayores niveles de escolaridad --- class: animated, fadeIn ### Especificación cuadrática **Relación entre puntaje académico y salarios** <img src="data:image/png;base64,#DCPP_semana2_files/figure-html/unnamed-chunk-59-1.png" width="60%" style="display: block; margin: auto;" /> -- - El gráfico sugiere que la relación entre ingreso y puntaje académico no es lineal **¿porqué?** --- class: animated, fadeIn ### Especificación cuadrática - Hagamos la regresión $$puntaje_i=\beta_0 + \beta_1 ingreso_i + \beta_2 ingreso_i^2 + \mu_i $$ ```r dw2 <- dw2 %>% mutate(wage2=wage^2) m8 <- lm_robust(IQ ~ wage + wage2, data=dw2, se_type = "stata") ``` ```r summary(m8) ``` ``` Call: lm_robust(formula = IQ ~ wage + wage2, data = dw2, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 84.83192627 2.31863776 36.59 0.000 80.282 89.38 932 wage 0.02219924 0.00390765 5.68 0.000 0.015 0.03 932 wage2 -0.00000445 0.00000153 -2.92 0.004 0.000 0.00 932 Multiple R-squared: 0.103 , Adjusted R-squared: 0.101 F-statistic: 52.4 on 2 and 932 DF, p-value: <0.0000000000000002 ``` --- class: animated, fadeIn ### Especificación cuadrática + Modelo propuesto: $$puntaje_i=\beta_0 + \beta_1 ingreso_i + \beta_2 ingreso_i^2 + \mu_i $$ `$$puntaje_i=84.83 + 0.022 ingreso_i + (-0.000005) ingreso_i^2$$` <table> <thead> <tr> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Ingreso </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Predicción del Modelo </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Cambio en el score </th> </tr> </thead> <tbody> <tr> <td style="text-align:center;background-color: white !important;"> 100 </td> <td style="text-align:center;background-color: white !important;"> 87.0 </td> <td style="text-align:center;background-color: white !important;"> </td> </tr> <tr> <td style="text-align:center;background-color: white !important;background-color: white !important;"> 101 </td> <td style="text-align:center;background-color: white !important;background-color: white !important;"> 87.0 </td> <td style="text-align:center;background-color: white !important;background-color: white !important;"> 0.021 </td> </tr> <tr> <td style="text-align:center;"> 200 </td> <td style="text-align:center;"> 89.0 </td> <td style="text-align:center;"> </td> </tr> <tr> <td style="text-align:center;background-color: white !important;"> 201 </td> <td style="text-align:center;background-color: white !important;"> 89.0 </td> <td style="text-align:center;background-color: white !important;"> 0.020 </td> </tr> <tr> <td style="text-align:center;"> 400 </td> <td style="text-align:center;"> 92.8 </td> <td style="text-align:center;"> </td> </tr> <tr> <td style="text-align:center;"> 401 </td> <td style="text-align:center;"> 92.8 </td> <td style="text-align:center;"> 0.018 </td> </tr> </tbody> </table> --- class: animated, fadeIn ### Especificación cuadrática - Hagamos la regresión $$puntaje_i=\beta_0 + \beta_1 ingreso_i + \mu_i $$ ```r m8b <- lm_robust(IQ ~ wage, data=dw2, se_type = "stata") ``` ```r summary(m8b) ``` ``` Call: lm_robust(formula = IQ ~ wage, data = dw2, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 90.2602 1.28079 70.47 0 87.747 92.774 933 wage 0.0115 0.00121 9.54 0 0.009 0.014 933 Multiple R-squared: 0.0955 , Adjusted R-squared: 0.0946 F-statistic: 91.1 on 1 and 933 DF, p-value: <0.0000000000000002 ``` --- class: animated, fadeIn ### Especificación cuadrática + Modelo propuesto: $$puntaje_i=\beta_0 + \beta_1 ingreso_i + \mu_i $$ $$puntaje_i=90.26 + 0.012 ingreso_i $$ <table> <thead> <tr> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Ingreso </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Predicción del Modelo </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Cambio en el score </th> </tr> </thead> <tbody> <tr> <td style="text-align:center;background-color: white !important;"> 100 </td> <td style="text-align:center;background-color: white !important;"> 92.6 </td> <td style="text-align:center;background-color: white !important;"> </td> </tr> <tr> <td style="text-align:center;background-color: white !important;background-color: white !important;"> 101 </td> <td style="text-align:center;background-color: white !important;background-color: white !important;"> 92.6 </td> <td style="text-align:center;background-color: white !important;background-color: white !important;"> 0.023 </td> </tr> <tr> <td style="text-align:center;"> 200 </td> <td style="text-align:center;"> 94.9 </td> <td style="text-align:center;"> </td> </tr> <tr> <td style="text-align:center;background-color: white !important;"> 201 </td> <td style="text-align:center;background-color: white !important;"> 94.9 </td> <td style="text-align:center;background-color: white !important;"> 0.023 </td> </tr> <tr> <td style="text-align:center;"> 400 </td> <td style="text-align:center;"> 99.5 </td> <td style="text-align:center;"> </td> </tr> <tr> <td style="text-align:center;"> 401 </td> <td style="text-align:center;"> 99.5 </td> <td style="text-align:center;"> 0.023 </td> </tr> </tbody> </table> --- class: animated, fadeIn ### Variable dependiente binaria (VDB) - Antes ocupamos variables dummy como variables independientes en una regresión - Pero también se utilizan ampliamente cómo variables dependientes - Trabaja no trabaja - Tiene o no tiene seguro - Entre o no a la Universidad - Es pobre o no - Hay tres formas de estimar una regresión con VDB - MCO: Modelo de probabilidad lineal - Probit - Logit --- name: mpl class: animated, fadeIn ### Modelo de probabilidad lineal - El MPL es una regresión múltiple del tipo: `$$Y_i=\beta_0 + \beta_1X_{1i} + \beta_2X_{2i} + \ldots + \beta_kX_{ki} + \mu_i$$` - Done la única diferencia es que ahora `\(Y_i\)` es binaria: `\(\in \{0,1\}\)` - `\(E[Y| X] = 1Pr(Y=1|X) + 0Pr(Y=0|X)=Pr(Y=1|X)\)` - `\(Pr(Y=1|X) = \beta_0 + \beta_1X_{1i} + \beta_2X_{2i} + \ldots + \beta_kX_{ki}\)` - `\(\beta_k\)`: Cambio predicho en la probabilidad de éxito `\((Y_i=1)\)` cuando `\(X_k\)` aumenta en una unidad, dejando todas las demás variables constante. --- class: animated, fadeIn ### Modelo de probabilidad lineal: ejemplo Se utiliza el mismo método que hemos usado hasta ahora: MCO Ejemplo: Determinantes del trabajo admninistrativo `$$PRF: CLEROCC_i=\beta_0 + \beta_1EDUC_{i} + \beta_2FEMALE_{i} + \beta_3NONWHITE_{i} + \mu_i$$` ```r dw %>% select(clerocc, educ, female, nonwhite) %>% head() ``` ``` clerocc educ female nonwhite 1 0 11 1 0 2 0 12 1 0 3 0 11 0 0 4 1 8 0 0 5 0 12 0 0 6 0 16 0 0 ``` --- class: animated, fadeIn ### Modelo de probabilidad lineal: ejemplo Ejemplo: Determinantes del trabajo admninistrativo `$$PRF: CLEROCC_i=\beta_0 + \beta_1EDUC_{i} + \beta_2FEMALE_{i} + \beta_3NONWHITE_{i} + \mu_i$$` ```r m9 <- lm_robust(clerocc ~ educ + female + nonwhite, data=dw, se_type = "stata") ``` -- ```r summary(m9) ``` ``` Call: lm_robust(formula = clerocc ~ educ + female + nonwhite, data = dw, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 0.01009 0.0578 0.175 0.861 -0.103 0.124 522 educ 0.00264 0.0044 0.600 0.549 -0.006 0.011 522 female 0.26642 0.0315 8.448 0.000 0.204 0.328 522 nonwhite -0.03516 0.0471 -0.747 0.455 -0.128 0.057 522 Multiple R-squared: 0.127 , Adjusted R-squared: 0.122 F-statistic: 23.9 on 3 and 522 DF, p-value: 0.0000000000000162 ``` --- class: animated, fadeIn ### Modelo de probabilidad lineal - La principal ventaja del MPL es que no requiere aprender un nuevo método - La interpretación de los coeficientes es directa - Un problema es que podemos obtener valore predichos fuero del intervalo `\([0,1]\)` - Esta es una de las crÃticas principales a usar MCO para un modelo de variable dependiente binaria y abogan por *logit* o *probit* - Pero, si no estamos interesadas(os) en `\(\hat{Y}\)` pero en cambio en `\(\Delta Y\)` MPL puede hacerlo bastante bien - Además MPL tiene un buen desempeño cuando queremos hacer predicciones evaluando las variables en el promedio - En la práctica MPL se usa cómo primera aproximación a la estimación y luego se utiliza algo más sofisticado. --- class: animated, fadeIn ### Ejemplo Graficamente <img src="data:image/png;base64,#02_img/fig3.png" width="50%" style="display: block; margin: auto;" /> --- name: probit class: animated, fadeIn ### Probit - Modelo no lineal - Valores predichos entre 0 y 1 - Valores predichos similares a MPL cuando evaluamos en el promedio, pero muy distintos para otros valores de las Xs - El modelo tiene la siguiente representación: - `\(Pr(Y=1 | X) = \Phi(\beta_0 + \beta_1X_{1i} + \beta_2X_{2i} + \ldots + \beta_kX_{ki})\)` - Donde `\(Y\)` es binaria y `\(\Phi\)` es la función de distribución acumulada de una normal con regresores `\(X_1, X_2, \ldots\)` - Los coeficientes del *probit*, `\(\beta_0, \beta_1\)`, etc no tienen interpretación directa. El modelo se interpreta mejor computando los valores predichos de `\(Y_i\)` con una cambio en el regresor `\(X\)` - La probabilidad de que `\(Y=1\)` dado valores de `\(X_1, X_2, \ldots, X_k\)` se computan calculando los valores `\(z = \beta_0 + \beta_1X_{1i} + \beta_2X_{2i} + \ldots + \beta_kX_{ki}\)` y luego buscando este valor `\(z\)` en la tabla de distribución normal --- name: logit class: animated, fadeIn ### Logit - Modelo no lineal - Valores predichos entre 0 y 1 - Valores predichos similares a MPL cuando evaluamos en el promedio, pero muy distintos para otros valores de las Xs - El modelo tiene la siguiente representación: `$$Pr(Y=1 | X) = F(\beta_0 + \beta_1X_{1i} + \beta_2X_{2i} + \ldots + \beta_kX_{ki})$$` `$$=\frac{1}{1-exp(\beta_0 + \beta_1X_{1i} + \beta_2X_{2i} + \ldots + \beta_kX_{ki})}$$` - Los coeficientes del *logit*, `\(\beta_0, \beta_1\)`, etc no tienen interpretación directa. --- class: animated, fadeIn ### Modelos de VDB - Salvo en el caso del MPL los coeficientes no tienen interpretación directa cuando salen en el output de stata - Esto es igual que en modelos no lineales - El signo y la significancia estadÃstica si salen directo el output de Stata - En `R`: `glm(..., family=binomial(link="logit"))` reporta coeficientes; `logitmfx()` cambios en probabilidad; `predict()` para probabilidades predichas. - En `R`: `glm(..., family=binomial(link="probit"))` reporta coeficientes; `probitmfx()` cambios en probabilidad; `predict()` para probabilidades predichas. --- class: animated, fadeIn ### Modelos de VDB - ¿Cómo pensar en el cambio en `\(Y\)` que viene de un cambio en `\(X\)`? - Para cada modelo podemos hacer los siguientes pasos: 1. Computar la probabilidad predicha para el valor inicial de los regresores 2. Computar la probabilidad predicha para el nuevo valor de los regresores 3. Computar diferencia en (2) - (1) --- class: animated, fadeIn ### Ejemplo Participación laboral femenina. **Datos** + `participation`: 1=participa; 0=no participa + `youngkids`: número de hijos + `education`: años de educación formal <table class="table table-striped table-hover table-condensed table-responsive" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Variable </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Obs </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Media </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Std </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Min </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Max </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;background-color: white !important;"> participation </td> <td style="text-align:center;background-color: white !important;"> 872 </td> <td style="text-align:center;background-color: white !important;"> 0.46 </td> <td style="text-align:center;background-color: white !important;"> 0.499 </td> <td style="text-align:center;background-color: white !important;"> 0 </td> <td style="text-align:center;background-color: white !important;"> 1 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> kids </td> <td style="text-align:center;background-color: white !important;"> 872 </td> <td style="text-align:center;background-color: white !important;"> 1.29 </td> <td style="text-align:center;background-color: white !important;"> 1.113 </td> <td style="text-align:center;background-color: white !important;"> 0 </td> <td style="text-align:center;background-color: white !important;"> 6 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> education </td> <td style="text-align:center;background-color: white !important;"> 872 </td> <td style="text-align:center;background-color: white !important;"> 9.31 </td> <td style="text-align:center;background-color: white !important;"> 3.036 </td> <td style="text-align:center;background-color: white !important;"> 1 </td> <td style="text-align:center;background-color: white !important;"> 21 </td> </tr> </tbody> </table> --- class: animated, fadeIn ### MPL **Ejemplo**: Participación laboral femenina. ```r m10 <- lm_robust(participation ~ kids, data=dl, se_type = "stata") ``` ```r summary(m10) ``` ``` Call: lm_robust(formula = participation ~ kids, data = dl, se_type = "stata") Standard error type: HC1 Coefficients: Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF (Intercept) 0.46968 0.0260 18.069 0.000 0.419 0.521 870 kids -0.00758 0.0152 -0.498 0.619 -0.037 0.022 870 Multiple R-squared: 0.000287 , Adjusted R-squared: -0.000863 F-statistic: 0.248 on 1 and 870 DF, p-value: 0.619 ``` --- class: animated, fadeIn ### MPL - ¿Cuál es la diferencia en la probabilidad predicha de participar en el mercado laboral para una mujer con 3 hijos relativo a una mujer con 2 hijos? + Mujer con dos 2 hijos: ```r m10$coefficients[1] + m10$coefficients[2]*2 ``` ``` (Intercept) 0.455 ``` + Mujer con dos 3 hijos: ```r m10$coefficients[1] + m10$coefficients[2]*3 ``` ``` (Intercept) 0.447 ``` **Discusión en clases**: ¿Cuál es la diferencia en la probabilidad predicha de participar en el mercado laboral para una mujer con 6 hijos relativo a una mujer con 4 hijos? --- class: animated, fadeIn ### Probit **Ejemplo**: Participación laboral femenina. ```r m11 <- glm(participation ~ kids, data=dl, family = binomial(link="probit")); summary(m11) ``` ``` Call: glm(formula = participation ~ kids, family = binomial(link = "probit"), data = dl) Deviance Residuals: Min 1Q Median 3Q Max -1.13 -1.11 -1.09 1.24 1.31 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) -0.0761 0.0652 -1.17 0.24 kids -0.0191 0.0382 -0.50 0.62 (Dispersion parameter for binomial family taken to be 1) Null deviance: 1203.2 on 871 degrees of freedom Residual deviance: 1203.0 on 870 degrees of freedom AIC: 1207 Number of Fisher Scoring iterations: 3 ``` --- class: animated, fadeIn ### Probit **Ejemplo**: Participación laboral femenina. - ¿Cuál es la diferencia en la probabilidad predicha de participar en el mercado laboral para una mujer con 3 hijos relativo a una mujer con 2 hijos? .pull-left[ + Mujer con 2 hijos: `\(F(\hat{\beta_0} + \hat{\beta_1}X)=F(-0.0761 + -0.0191*kids)=\)` `\(F(-0.0761 + -0.0191*kids)0\)` `\(F(-0.0761 + -0.0191*2)=F(-0.114)=\)` `\(F(-0.114)=F(z\leq-0.114)=0.545\)` ```r pnorm(-0.114, mean = 0, sd = 1, lower.tail = TRUE) ``` ``` [1] 0.455 ``` ] .pull-right[ + Mujer con 3 hijos: `\(F(\hat{\beta_0} + \hat{\beta_1}X)=F(-0.0761 + -0.0191*kids)=\)` `\(F(-0.0761 + -0.0191*kids)=\)` `\(F(-0.0761 + -0.0191*3)=F(-0.133)=\)` `\(F(-0.133)=F(z\leq-0.133)=0.553\)` ```r pnorm(-0.133, mean = 0, sd = 1, lower.tail = TRUE) ``` ``` [1] 0.447 ``` ] -- **Discusión en clase**: ¿Cuál es la diferencia en la probabilidad predicha de participar en el mercado laboral para una mujer con 6 hijos relativo a una mujer con 4 hijos? --- class: animated, fadeIn ### Logit **Ejemplo**: Participación laboral femenina. ```r m12 <- glm(participation ~ kids, data=dl, family = binomial(link="logit")) summary(m12) ``` ``` Call: glm(formula = participation ~ kids, family = binomial(link = "logit"), data = dl) Deviance Residuals: Min 1Q Median 3Q Max -1.13 -1.11 -1.09 1.24 1.31 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) -0.1214 0.1042 -1.16 0.24 kids -0.0306 0.0611 -0.50 0.62 (Dispersion parameter for binomial family taken to be 1) Null deviance: 1203.2 on 871 degrees of freedom Residual deviance: 1203.0 on 870 degrees of freedom AIC: 1207 Number of Fisher Scoring iterations: 3 ``` -- - ¿Cuál es la diferencia en la probabilidad predicha de participar en el mercado laboral para una mujer con 3 hijos relativo a una meujer con 2 hijos? - ¿Cuál es la diferencia en la probabilidad predicha de participar en el mercado laboral para una mujer con 6 hijos relativo a una mujer con 4 hijos? --- class: animated, fadeIn ### Logit **Ejemplo**: Participación laboral femenina. - ¿Cuál es la diferencia en la probabilidad predicha de participar en el mercado laboral para una mujer con 3 hijos relativo a una mujer con 2 hijos? .pull-left[ + Mujer con 2 hijos: `\(\frac{1}{1-exp(\hat{\beta_0} + \hat{\beta_1}X)}=\)` `\(\frac{1}{1-exp(-0.1214 + -0.0306*2)}\)` ```r 1/(1+exp(-1*(-0.1214 + -0.0306*2))) ``` ``` [1] 0.454 ``` ```r predict(m12, data.frame(kids=2), type = "response") ``` ``` 1 0.455 ``` ] .pull-right[ + Mujer con 3 hijos: `\(\frac{1}{1-exp(\hat{\beta_0} + \hat{\beta_1}X)}=\)` `\(\frac{1}{1-exp(-0.1214 + -0.0306*3)}\)` ```r 1/(1+exp(-1*(-0.1214 + -0.0306*3))) ``` ``` [1] 0.447 ``` ```r predict(m12, data.frame(kids=3), type = "response") ``` ``` 1 0.447 ``` ] -- **Discusión en clase**: ¿Cuál es la diferencia en la probabilidad predicha de participar en el mercado laboral para una mujer con 6 hijos relativo a una mujer con 4 hijos? --- class: animated, fadeIn ## Comparando modelos <br> <table class="table table-striped table-hover table-condensed table-responsive" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Probabilidad Predicha </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> MPL </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Probit </th> <th style="text-align:center;font-weight: bold;color: white !important;background-color: #586CC4 !important;"> Logit </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;background-color: white !important;"> Kids=2 </td> <td style="text-align:center;background-color: white !important;"> 0.4545 </td> <td style="text-align:center;background-color: white !important;"> 0.4545 </td> <td style="text-align:center;background-color: white !important;"> 0.4545 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> Kids=3 </td> <td style="text-align:center;background-color: white !important;"> 0.4469 </td> <td style="text-align:center;background-color: white !important;"> 0.447 </td> <td style="text-align:center;background-color: white !important;"> 0.4469 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> Diff </td> <td style="text-align:center;background-color: white !important;"> -0.0076 </td> <td style="text-align:center;background-color: white !important;"> -0.0076 </td> <td style="text-align:center;background-color: white !important;"> -0.0076 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> </td> <td style="text-align:center;background-color: white !important;"> </td> <td style="text-align:center;background-color: white !important;"> </td> <td style="text-align:center;background-color: white !important;"> </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> </td> <td style="text-align:center;background-color: white !important;"> </td> <td style="text-align:center;background-color: white !important;"> </td> <td style="text-align:center;background-color: white !important;"> </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> </td> <td style="text-align:center;background-color: white !important;"> </td> <td style="text-align:center;background-color: white !important;"> </td> <td style="text-align:center;background-color: white !important;"> </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> Kids=4 </td> <td style="text-align:center;background-color: white !important;"> 0.4393 </td> <td style="text-align:center;background-color: white !important;"> 0.4394 </td> <td style="text-align:center;background-color: white !important;"> 0.4394 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> Kids=6 </td> <td style="text-align:center;background-color: white !important;"> 0.4242 </td> <td style="text-align:center;background-color: white !important;"> 0.4244 </td> <td style="text-align:center;background-color: white !important;"> 0.4244 </td> </tr> <tr> <td style="text-align:left;background-color: white !important;"> Diff </td> <td style="text-align:center;background-color: white !important;"> -0.0152 </td> <td style="text-align:center;background-color: white !important;"> -0.015 </td> <td style="text-align:center;background-color: white !important;"> -0.015 </td> </tr> </tbody> </table> **Discusión en clases**: ¿Qué nos dicen las similitudes entre las diferencias estimadas para cada modelo? --- name: despedida class: inverse, center, middle background-image: url(data:image/png;base64,#02_img/logo-uc.png) background-position: 50% 10% background-size: 10%, cover <br><br><br> ## Semana 2 ### Especificaciones de Regresión Lineal 26 de septiembre, 2022 <div class="my-footer"></div>
<b>Pablo A. Celhay</b> | [
pacelhay@uc.cl](mailto:pacelhay@uc.cl) .left[.footnote[ <br> Diseño y formato de la presentación:
José Daniel Conejeros | [
jdconejeros@uc.cl](mailto:jdconejeros@uc.cl) | [
JDConejeros](https://github.com/JDConejeros) ]]